import base64
import requests
import oss2
from urllib.parse import urlparse
import json
from io import BytesIO

#oss
ossAuth = oss2.Auth('LTAI5tMzkcDHweKw7762CzQn', '6cfufuTuoQpdwR7mckhxITwkLBWtcH')
ossBucket = oss2.Bucket(ossAuth, 'oss-cn-hongkong.aliyuncs.com', 'aipainter-overseas') 

def getUrlFileBase64(imgUrl):
    response = requests.get(imgUrl)
    return base64.b64encode(BytesIO(response.content).read()).decode('utf-8')


def getFileBase64(imgUrl):
    with open(imgUrl, 'rb') as f:
        base64_data = base64.b64encode(f.read()).decode('utf-8')
    return base64_data

def delOssFile(img) :
    req = urlparse(img)
    img_path = req.path[1:]
    print(img_path)

    res = ossBucket.delete_object(img_path)
    print(res.status)

def getInterrogate(url,img,model):
    headers = {
        "User-Agent":model,
        'Content-Type': 'application/json'
    }

    payload = {
        "image": getUrlFileBase64(img),
        "model": "clip"
        #deepdanbooru,clip
    }

    payload_json_pro = json.dumps(payload)
    #sprint(payload_json_pro)
    responsePro = requests.post(url=f'{url}/sdapi/v1/interrogate', data=payload_json_pro, headers=headers).json()
    print(responsePro)

#getUrlFileBase64("https://overseas-files.dalleai.art/uploads/241603b1bf676c7e37b17b145fbbda50.png")

#getFileBase64('./d2a7fc96d4afc0d8a6f572dd28383d49.png')

#oss删除图片
img = "https://overseas-files.dalleai.art/uploads/08d93d5b1a2bd8cf727388dcca5a118c.png"
#delOssFile(img)

#img2img
getInterrogate(
    "http://region-8.seetacloud.com:27502",
    "https://overseas-files.dalleai.art/uploads/790858d57c03da5f2b173eed149c3b25.png!webp",
    "majicmixRealistic_v4"
)

#print(getFileBase64('./934b1d5f48cd3a33ad8821b7e29f26b6.png'))
#print(getUrlFileBase64("https://overseas-files.dalleai.art/uploads/934b1d5f48cd3a33ad8821b7e29f26b6.png"))


